package com.library.service.serviceImpl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.library.dto.pojo.PageBean;
import com.library.dto.pojo.excelPo.BookExportInfo;
import com.library.dto.pojo.excelPo.BookImportInfo;
import com.library.dto.vo.BookVO;
import com.library.entity.Book;
import com.library.entity.Booktype;
import com.library.mapper.BookMapper;
import com.library.mapper.BooktypeMapper;
import com.library.service.IBookService;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 至尊伍人组
 * @since 2018-09-30
 */
@Service
public class BookServiceImpl extends ServiceImpl<BookMapper, Book> implements IBookService {

    /*每页显示的记录条数*/
    private final int PAGESIZE = 10;

    @Autowired
    BooktypeMapper booktypeMapper;

    /**
     * 查询分页之后的图书记录
     *
     * @param pageNum 要查询的页数
     * @param book    封装模糊查询的条件的实体列
     * @return
     */
    @Override
    public PageBean<BookVO> getBookVoList( int pageNum, Book book ) {


        /*获得所有的书的种类*/
        List<Booktype> booktypes = booktypeMapper.selectList(null);
        //把所有的图书种类的list集合转换为map类型
        Map<Integer, String> typeMap = booktypes
                                           .stream()
                                           .collect(
                                               Collectors.toMap(Booktype::getTypeId,
                                                   Booktype::getTypeName)
                                           );

        EntityWrapper<Book> wrapper = null;
        /*如果传递的封装查询对象不为空，遍历要查询的条件的属性，把条件加到映射器中*/
        if ( book != null ) {

            System.out.println("模糊查询的封装对象:" + book);

            wrapper = new EntityWrapper<Book>();

            if ( book.getAuthor() != null && book.getAuthor().trim().length() != 0 ) {
                wrapper.like("author", book.getAuthor());
            }
            if ( book.getPublisher() != null && book.getPublisher().trim().length() != 0 ) {
                wrapper.like("publisher", book.getPublisher());
            }
            if ( book.getBookName() != null && book.getBookName().trim().length() != 0 ) {
                wrapper.like("bookName", book.getBookName());
            }
            if ( book.getIsbn() != null && book.getIsbn().trim().length() != 0 ) {
                wrapper.eq("isbn", book.getIsbn());
            }
            if ( book.getTypeId() != null && book.getTypeId() != 0 ) {
                wrapper.eq("typeId", book.getTypeId());
            }
            System.out.println(wrapper.toString());
        }
        /*获得所有的书籍（但是没有书籍种类名称）*/
        List<Book> books = super.selectPage(new Page<Book>(pageNum, PAGESIZE), wrapper).getRecords();

        //        System.out.println(books.size());

        //需要封装的视图对象
        List<BookVO> bookVoS = new ArrayList<>();

        books.forEach(e -> {
            BookVO bookVO = new BookVO();
            try {
                /*将书籍对象中的所有属性拷贝到书籍vo对象中*/
                BeanUtils.copyProperties(bookVO, e);
                bookVO.setTypeName(typeMap.get(bookVO.getTypeId()));

                bookVoS.add(bookVO);
            } catch ( Exception e1 ) {
                e1.printStackTrace();
            }
        });

        //获得中的记录条数
        int count = super.selectCount(null);

        //将数据还有页数等数据保存在分页对象中
        PageBean<BookVO> pageBean = new PageBean<>(pageNum, PAGESIZE, count);
        pageBean.setList(bookVoS);

        return pageBean;

    }

    public List<BookExportInfo> getBookExcelList() {


        /*获得所有的书的种类*/
        List<Booktype> booktypes = booktypeMapper.selectList(null);
        //把所有的图书种类的list集合转换为map类型
        Map<Integer, String> typeMap = booktypes
                                           .stream()
                                           .collect(Collectors.toMap(Booktype::getTypeId, Booktype::getTypeName));

        //获得所有的图书记录
        List<Book> books = super.selectList(null);

        //要封装的返回的对象集合
        List<BookExportInfo> bookExportInfos = new ArrayList<>();

        books.parallelStream()
            .forEach(e -> {
                BookExportInfo bookExportInfo = new BookExportInfo();
                try {
                    /*将书籍对象中的所有属性拷贝到bookInfo对象中*/
                    BeanUtils.copyProperties(bookExportInfo, e);
                    bookExportInfo.setTypeName(typeMap.get(bookExportInfo.getTypeId()));

                    bookExportInfos.add(bookExportInfo);
                } catch ( Exception e1 ) {
                }
            });

        return bookExportInfos;
    }

    /**
     * 将excel中获得的数据转换为对应的实体类对象，同时加入到数据库中
     *
     * @param objects
     */
    @Override
    public void insertBatchFromExcel( List<Object> objects ) {

        List<Book> books = new ArrayList<>();

        objects.parallelStream().forEach(e -> {
            BookImportInfo bookImportInfo = (BookImportInfo)e;

            Book book = new Book();

            try {
                BeanUtils.copyProperties(book, bookImportInfo);
                books.add(book);
            } catch ( Exception e1 ) {
            }

        });

        //批量插入到数据库中
        super.insertBatch(books);

//        books.forEach(System.out::println);
    }

}
